package cn.wecloud.im.multiplayervideodemo.http

import android.util.MalformedJsonException
import com.google.gson.JsonParseException
import io.reactivex.functions.Consumer
import org.json.JSONException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.ParseException


fun errorConsumer(
    next: (msg: String) -> Unit = {}
): Consumer<Throwable> {
    return Consumer { e ->
        val errorMsg = when (e) {
            is HttpException -> {  //服务端返回码非200
                e.msg
            }
            is SocketTimeoutException, is ConnectException, is UnknownHostException -> {
                "网络连接异常, 请检查网络是否连接"
            }
            is retrofit2.HttpException -> {
                "服务器正忙，请稍后再试"
            }
            is JsonParseException, is JSONException, is ParseException, is MalformedJsonException -> {
                "数据解析错误"
            }
            else -> {
                "未知错误，请稍后在试"
            }
        }
        next(errorMsg)
    }
}
